
import BaseDraw from "./BaseDraw";
const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@requireComponent(cc.Sprite)
/**
 * 需要一个白色的背景图片
 * 使用RenderTexture更新内容，优化drawcall。
 * 支持清理
 * 支持回退
 * 使用触摸点的坐标绘制图形
 */
export default class DrawV1 extends BaseDraw {

    private target: cc.Sprite = null;

    protected isBack: boolean = false;
    protected stepBuffer: any[][] = []
    protected tempBuffer: cc.Vec2[] = [];
    private initData: any;
    start() {
        super.start();
        this.target = this.getComponent(cc.Sprite)
        this.target.node.scaleY = -1;
        let count = this.renderNode.width * this.renderNode.height
        this.initData = new Uint8Array(count * 4)
        for (let index = 0; index < count; index++) {
            this.initData[index * 4] = 255;
            this.initData[index * 4 + 1] = 255;
            this.initData[index * 4 + 2] = 255;
            this.initData[index * 4 + 3] = 255;
        }
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchCancel, this)
    }





    touchCancel() {

        this.clearContent();

    }



    clearContent() {
        // this.stepBuffer.push()
        // this.textureHelper.render()
        // let data: any = this.textureHelper.getData()
        // let tTexture = this.target.spriteFrame.getTexture()
        // tTexture.initWithData(data, tTexture.getPixelFormat(), this.node.width, this.node.height)
        // this.stepBuffer.push(data)
        // this.isBack = false;
        // //回退次数限制
        // if (this.stepBuffer.length > 20) {
        //     this.stepBuffer.shift();
        // }
        this.tempBuffer.length = 0;
        // this.g.clear()
    }

    touchStart(e) {
        this.paintStart(this.node.convertToNodeSpaceAR(e.getLocation()))
    }

    touchMove(e: cc.Touch) {
        this.paintStart(this.node.convertToNodeSpaceAR(e.getLocation()))
        if (this.tempBuffer.length > 50) {
            // cc.log(' 优化处理')
            this.clearContent()
        }

    }


    onButtonBack() {
        if (!this.isBack) {
            if (this.stepBuffer.length > 0) {
                this.stepBuffer.pop();
                this.isBack = true;
            }
        }
        if (this.stepBuffer.length > 0) {
            let data: any = this.stepBuffer.pop();
            let tTexture = this.target.spriteFrame.getTexture()
            tTexture.initWithData(data, tTexture.getPixelFormat(), this.node.width, this.node.height)
        } else {
            let tTexture = this.target.spriteFrame.getTexture()
            tTexture.initWithData(this.initData, tTexture.getPixelFormat(), this.node.width, this.node.height)
        }
    }

    clear() {
        super.clear()
        this.stepBuffer.length = 0;
        let tTexture = this.target.spriteFrame.getTexture()
        tTexture.initWithData(this.initData, tTexture.getPixelFormat(), this.node.width, this.node.height)
    }


    getData() {
        if (this.stepBuffer.length > 0) {
            return this.stepBuffer[this.stepBuffer.length - 1]
        }
        return ''
    }

}
